package org.crc.myvhrb.controller.system;

import org.crc.myvhrb.model.Hr;
import org.crc.myvhrb.model.RespBean;
import org.crc.myvhrb.model.Role;
import org.crc.myvhrb.service.HrService;
import org.crc.myvhrb.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * CRC
 * 2021/8/12
 */
@RestController
@RequestMapping("/system/hr")
public class HrController {

    @Autowired
    HrService hrService;
    @Autowired
    RoleService roleService;

    /**
     *  获取所有 hr 数据
     * @param keywords 搜索字段
     * @return
     */
    @GetMapping("/")
    public List<Hr> getAllHrs(String keywords){
        List<Hr> allHrs = hrService.getAllHrs(keywords);
        return allHrs;
    }
    // 获取所有角色
    @GetMapping("/role")
    public List<Role> getAllRoles(){
        return roleService.getAllRoles();
    }

    // 更新用户
    @PutMapping("/")
    public RespBean updateHr(@RequestBody Hr hr){
        if (hrService.updateHr(hr) == 1){
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    // 更新用户角色
    @PutMapping("/role")
    public RespBean updateHrRole(Integer hrid,Integer[] rids){
        if (hrService.updateHrRole(hrid,rids)){
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

    // 删除用户
    @DeleteMapping("/{id}")
    public RespBean deleteHr(@PathVariable Integer id){
        if (hrService.deleteHr(id)==1){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }
}
